using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(BakedShadow))]
public class BakedShadowEditor : Editor
{
    BakedShadow _BakedShadow;

    void OnEnable() => _BakedShadow = (BakedShadow)target;

    public override void OnInspectorGUI()
    {
        if (target == null)
        {
            return;
        }

        base.OnInspectorGUI();
        
        serializedObject.Update();
        
        DoBakedShadow();

        RefreshShadow();
        
        SceneView.RepaintAll();
        serializedObject.ApplyModifiedProperties();
    }
    
    private void DoBakedShadow()
    {
        if (GUILayout.Button("烘培阴影Volume"))
        {
            _BakedShadow.DoBakedShadow();
        }
    }

    private void RefreshShadow()
    {
        if (GUILayout.Button("使用Volume阴影"))
        {
            Shader.EnableKeyword("_USE_IVGI");
            Shader.DisableKeyword("_USE_IVGI_VERTEX");
            
            Shader.SetGlobalTexture("_IVGI_ShadowMap", _BakedShadow._ShadowMap3D);
            Shader.SetGlobalVector("_VolumeOrigin", Vector4.zero);
            Shader.SetGlobalFloat("_VolumeSize", 100);
        }
        
        if (GUILayout.Button("使用原始阴影"))
        {
            Shader.DisableKeyword("_USE_IVGI");
            Shader.DisableKeyword("_USE_IVGI_VERTEX");
        }
    }
}
